﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using Access.PowerTools.AbstractionLayer;

namespace Access.PowerTools.TestConsole
{
    [TestFixture()]
    public class TestSet_0002_SimulationObjects
    {
        [SetUp]
        public void Setup()
        {
            Access.PowerTools.Settings.Globals.Init();
            Access.PowerTools.Settings.Globals.SimulationModeOn = true;

            Access.PowerTools.Settings.Globals.HostApplication =
                new Access.PowerTools.Simulation.Engine.SimulationCommunicationBridge();
        }

        [TearDown]
        public void TearDown() { }
        [Test]public void Test_001_AccessNameAndVersion()
        {
            Access.PowerTools.AbstractionLayer.AccessApplication
                app = new Access.PowerTools.AbstractionLayer.AccessApplication(); 

            string name = app.Name;
            string version = app.Version;

            System.Console.WriteLine("Name = {0}, Version = {1}", name, version);  

            bool ret = (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(version))   ;

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_002_BackUpFileName()
        {
            string tableName = "Categories";
            string fileName =
              AccessDatabaseObjectInfo.GetBackUpFileName(AccessDataBaseObjectTypeEnumeration.Table,
                tableName);

            System.Console.WriteLine(fileName);

            bool ret = (fileName == "TABLE_" + tableName + ".xml");
            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_003_BackUpFileName_UndefinedObjectType()
        {
            string tableName = "Categories";
            string fileName = null;
            string message = null;
            try
            {
                fileName = AccessDatabaseObjectInfo.GetBackUpFileName((AccessDataBaseObjectTypeEnumeration)10,
                    tableName);
            }
            catch (Exception ex) 
            {
                message = ex.Message; 
            };
 
            System.Console.WriteLine(message);

            bool ret = (!string.IsNullOrEmpty(message));

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_004_SimulationSeedFileExists()
        {
            string tableName = "Categories";

            AccessDatabaseObjectInfo myObject =
                new AccessDatabaseObjectInfo(
                    AccessDataBaseObjectTypeEnumeration.Table, 
                    tableName);

            string objectFileFullPath
                 = myObject.SimulationSeedObjectInfoFileFullPath;

            Console.WriteLine(objectFileFullPath);
  
            bool ret = System.IO.File.Exists(objectFileFullPath);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_005_SimulationSeedFilesExist()
        {
            string seedFolder =
            Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder;
            string[] files = System.IO.Directory.GetFiles(seedFolder);
            Dictionary<string, string> simulationSeedFiles = new Dictionary<string, string>();

            foreach (string file in files)
            {
                string fileName = System.IO.Path.GetFileName(file);
                simulationSeedFiles.Add(fileName, fileName);
                //Console.WriteLine("{0}", fileName);
            }

            //Console.WriteLine("=====================");
  
            string[] mustHaveFiles = seedFilesList;
            bool test = false;

            foreach (string file in mustHaveFiles)
            {
                string fileName;
                test = simulationSeedFiles.TryGetValue(file, out fileName);
                if (!test)
                {
                    Console.WriteLine("NOT FOUND = {0}", file);
                    break;
                }
                //Console.WriteLine("{0}", fileName);  
            }


            bool ret = test;
            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_006_GetObjectTypeFromFileName()
        {
            AccessDataBaseObjectTypeEnumeration objectType =
            AccessDatabaseObjectInfo.GetObjectTypeByFileName("FORM_Categories.txt");

            Console.WriteLine("{0} - {1}", objectType, "FORM_Categories.txt");

            bool ret = (objectType == AccessDataBaseObjectTypeEnumeration.Form);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_007_GetObjectNameFromFileName()
        {
            string objectName =
            AccessDatabaseObjectInfo.GetObjectNameFromFileName("FORM_Categories.txt");

            Console.WriteLine("{0} - {1}", objectName, "FORM_Categories.txt");

            bool ret = (objectName == "Categories");

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_008_AllObjectsCount()
        {
            Access.PowerTools.AbstractionLayer.AccessApplication
                app = new Access.PowerTools.AbstractionLayer.AccessApplication();

            AccessDatabase database = app.CurrentDatabase ;
            int count = database.AllObjects.Count;

            foreach (AccessDatabaseObjectInfo o in database.AllObjects)
            {
                Console.WriteLine("{0} - {1}", o.Name, o.Type);   
            }

            System.Console.WriteLine("Count = {0}", count);

            bool ret = (count == 73);
            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }
        [Test]public void Test_009_AllObjectsTypesCount()
        {
            Access.PowerTools.AbstractionLayer.AccessApplication
                app = new Access.PowerTools.AbstractionLayer.AccessApplication();

            AccessDatabase database = app.CurrentDatabase;
            int count = database.AllObjects.Count;
            System.Console.WriteLine("Count = {0}", count);
            System.Console.WriteLine("Tables.Count = {0}", database.AllTables.Count);
            System.Console.WriteLine("Queries.Count = {0}", database.AllQueries.Count);
            System.Console.WriteLine("Forms.Count = {0}", database.AllForms.Count);
            System.Console.WriteLine("Reports.Count = {0}", database.AllReports.Count);
            System.Console.WriteLine("Macros.Count = {0}", database.AllMacros.Count);
            System.Console.WriteLine("Modules.Count = {0}", database.AllModules.Count);

            //Count = 73
            //Tables.Count = 8
            //Queries.Count = 21
            //Forms.Count = 22
            //Reports.Count = 14
            //Macros.Count = 6
            //Modules.Count = 2

            bool ret = (count == 73 &&
                   database.AllTables.Count == 8 &&
                   database.AllQueries.Count == 21 &&
                   database.AllForms.Count == 22 &&
                   database.AllReports.Count == 14 &&
                   database.AllMacros.Count == 6 &&
                   database.AllModules.Count == 2
                   );

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_010_ListAllObjectsByType()
        {
            Access.PowerTools.AbstractionLayer.AccessApplication
                app = new Access.PowerTools.AbstractionLayer.AccessApplication();

            AccessDatabase database = app.CurrentDatabase;
            int count = database.AllObjects.Count;

            foreach (AccessDatabaseObjectInfo accObject in database.AllObjects)
            {
                System.Console.WriteLine("{0} - {1} - {2}", 
                    accObject.Type, accObject.Name, accObject.BackUpFileName);  
            }

            bool ret = (count == 73);
            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        #region Seed Files
        private string[] seedFilesList
        {
            get
            {
                string[] files =
                {
                    "FORM_Categories.txt",
                    "FORM_Customer Labels Dialog.txt",
                    "FORM_Customer Orders Subform1.txt",
                    "FORM_Customer Orders Subform2.txt",
                    "FORM_Customer Orders.txt",
                    "FORM_Customer Phone List.txt",
                    "FORM_Customers.txt",
                    "FORM_Employees.txt",
                    "FORM_Main Switchboard.txt",
                    "FORM_Orders Subform.txt",
                    "FORM_Orders.txt",
                    "FORM_Product List.txt",
                    "FORM_Products.txt",
                    "FORM_Quarterly Orders Subform.txt",
                    "FORM_Quarterly Orders.txt",
                    "FORM_Sales Analysis Subform1.txt",
                    "FORM_Sales Analysis Subform2.txt",
                    "FORM_Sales Analysis.txt",
                    "FORM_Sales by Year Dialog.txt",
                    "FORM_Sales Reports Dialog.txt",
                    "FORM_Startup.txt",
                    "FORM_Suppliers.txt",
                    "MACRO_Customer Labels Dialog.txt",
                    "MACRO_Customer Phone List.txt",
                    "MACRO_Customers.txt",
                    "MACRO_Sales Totals by Amount.txt",
                    "MACRO_Sample Autokeys.txt",
                    "MACRO_Suppliers.txt",
                    "MODULE_Startup.txt",
                    "MODULE_Utility Functions.txt",
                    "QUERY_Alphabetical List of Products.txt",
                    "QUERY_Category Sales for 1997.txt",
                    "QUERY_Current Product List.txt",
                    "QUERY_Customers and Suppliers by City.txt",
                    "QUERY_Employee Sales by Country.txt",
                    "QUERY_Invoices Filter.txt",
                    "QUERY_Invoices.txt",
                    "QUERY_Order Details Extended.txt",
                    "QUERY_Order Subtotals.txt",
                    "QUERY_Orders Qry.txt",
                    "QUERY_Product Sales for 1997.txt",
                    "QUERY_Products Above Average Price.txt",
                    "QUERY_Products by Category.txt",
                    "QUERY_Quarterly Orders by Product.txt",
                    "QUERY_Quarterly Orders.txt",
                    "QUERY_Sales by Category.txt",
                    "QUERY_Sales by Year.txt",
                    "QUERY_Sales Totals by Amount.txt",
                    "QUERY_Summary of Sales by Quarter.txt",
                    "QUERY_Summary of Sales by Year.txt",
                    "QUERY_Ten Most Expensive Products.txt",
                    "REPORT_Alphabetical List of Products.txt",
                    "REPORT_Catalog Subreport.txt",
                    "REPORT_Catalog.txt",
                    "REPORT_Customer Labels.txt",
                    "REPORT_Employee Sales by Country.txt",
                    "REPORT_Invoice.txt",
                    "REPORT_Products by Category.txt",
                    "REPORT_Sales by Category Subreport.txt",
                    "REPORT_Sales by Category.txt",
                    "REPORT_Sales by Year Subreport.txt",
                    "REPORT_Sales by Year.txt",
                    "REPORT_Sales Totals by Amount.txt",
                    "REPORT_Summary of Sales by Quarter.txt",
                    "REPORT_Summary of Sales by Year.txt",
                    "TABLE_Categories.xml",
                    "TABLE_CategoriesSchema.xml",
                    "TABLE_Customers.xml",
                    "TABLE_CustomersSchema.xml",
                    "TABLE_Employees.xml",
                    "TABLE_EmployeesSchema.xml",
                    "TABLE_Order Details.xml",
                    "TABLE_Order DetailsSchema.xml",
                    "TABLE_Orders.xml",
                    "TABLE_OrdersSchema.xml",
                    "TABLE_Products.xml",
                    "TABLE_ProductsSchema.xml",
                    "TABLE_Shippers.xml",
                    "TABLE_ShippersSchema.xml",
                    "TABLE_Suppliers.xml",
                    "TABLE_SuppliersSchema.xml"
                };

                return files;
            }
        }
        #endregion
    }
}

